import { Tabs } from 'antd/lib'
import { connect } from 'dva'

import AekLinkButton from '@/components/AekLinkButton'
import AekTable from '@/components/AekTable'
import PageLayout from '@/components/PageLayout'
import SearchForm from '@/components/SearchForm'
import { namespace } from '@/models/pcs/pcsOrderStatistics'
import { getPagination } from '@/utils/antdHelper'
import { formatDate } from '@/utils/displayHelper'
import { getBasicFn } from '@/utils/dvaUtil/index'
import {
  PCS_COMBO_TYPE_ENUM,
  PCS_COMPANION_PERIOD_ENUM,
  PCS_COMPANION_ORDER_STATUS_ENUM,
  PCS_ORDER_STATISTICS_TAB_ENUM,
} from '@/utils/enum'

import DetailDrawer from '../pcsCompanionOrder/components/DetailDrawer'

import InfoModal from './components/InfoModal'

/** 陪护 - 订单统计 */
const Index = ({ modelState, loading }) => {
  const { pageList, pagination, searchParams, status } = modelState
  const { toAction, getLoading } = getBasicFn({ namespace, loading })

  const onActiveKeyChange = activeKey => {
    toAction({ status: activeKey, pagination: { current: 1 } }, true)
    toAction('getPageList')
  }

  const handleOrderCount = async row => {
    await InfoModal.PromiseThen({ id: row.nursingWorkersId })
  }

  /** @type {import("@/components/SearchForm").SearchFormProps} */
  const searchProps = {
    searchParams,
    formData: [
      {
        field: 'nursingWorkersName',
        label: '护工姓名',
        component: {
          name: 'Input',
        },
      },
      {
        exclude: status !== PCS_ORDER_STATISTICS_TAB_ENUM.DETAIL,
        field: 'orderNo',
        label: '订单号',
        component: {
          name: 'Input',
        },
      },
      {
        field: 'createDateRange',
        label: '创建日期',
        component: {
          name: 'RangePicker',
        },
      },
      {
        field: 'serviceStartDateRange',
        label: '服务开始时间',
        component: {
          name: 'RangePicker',
        },
      },
      {
        field: 'serviceEndDateRange',
        label: '服务结束时间',
        component: {
          name: 'RangePicker',
        },
      },
    ],
    onSearch: values => {
      toAction({ searchParams: values })
      toAction({ pagination: { current: 1 } }, true)
      toAction('getPageList')
    },
  }

  /** @type {import("@/components/AekTable").AekTableProps} */
  const tableProps = {
    configKey: namespace,
    rowKey:
      status === PCS_ORDER_STATISTICS_TAB_ENUM.PERSON
        ? 'nursingWorkersId'
        : ({ orderId, nursingWorkersName }) => `${orderId}-${nursingWorkersName}`,
    loading: getLoading('getPageList'),
    dataSource: pageList,
    pagination: getPagination(pagination, (current, pageSize) => {
      toAction('getPageList', { current, pageSize })
    }),
    columns:
      status !== PCS_ORDER_STATISTICS_TAB_ENUM.DETAIL
        ? [
            { dataIndex: 'nursingWorkersName', title: '姓名' },
            {
              dataIndex: 'orderCount',
              title: '订单数量',
              render: (text, row) => <AekLinkButton onClick={() => handleOrderCount(row)}>{text}</AekLinkButton>,
            },
            { dataIndex: 'totalAmount', title: '订单总费用', isMoney: true },
            { dataIndex: 'ettleTotalAmount', title: '结算总费用', isMoney: true },
          ]
        : [
            {
              dataIndex: 'orderNo',
              title: '订单号',
              render: (text, row) => (
                <AekLinkButton
                  onClick={() => {
                    DetailDrawer.PromiseThen({ id: row.orderId, hideOperate: true })
                  }}
                >
                  {text}
                </AekLinkButton>
              ),
            },
            { dataIndex: 'status', title: '订单状态', enum: PCS_COMPANION_ORDER_STATUS_ENUM },
            { dataIndex: 'patientName', title: '患者姓名' },
            {
              dataIndex: 'inpatientWardName',
              title: '地点',
              render: (text, row) => `${text} - ${row.bedNumber}床`,
            },
            { dataIndex: 'companionComboName', title: '套餐名称' },
            { dataIndex: 'comboType', title: '套餐类型', enum: PCS_COMBO_TYPE_ENUM },
            { dataIndex: 'nursingWorkersName', title: '姓名' },
            { dataIndex: 'chargingStandard', title: '收费标准' },
            { dataIndex: 'createTime', title: '工单创建时间', isDateTime: true },
            {
              dataIndex: 'startDate',
              title: '服务开始时间',
              render: (text, { startPeriod }) =>
                `${formatDate(text)} ${PCS_COMPANION_PERIOD_ENUM.getText(startPeriod)}`,
            },
            {
              dataIndex: 'endDate',
              title: '服务结束时间',
              render: (text, { endPeriod }) => `${formatDate(text)} ${PCS_COMPANION_PERIOD_ENUM.getText(endPeriod)}`,
            },
            { dataIndex: 'serviceDays', title: '服务时长' },
            { dataIndex: 'totalAmount', title: '订单费用', isMoney: true },
            { dataIndex: 'nursingWorkerSettlement', title: '护工结算' },
            { dataIndex: 'settleTotalAmount', title: '结算费用', isMoney: true },
          ],
  }

  return (
    <PageLayout>
      <Tabs
        activeKey={status}
        items={PCS_ORDER_STATISTICS_TAB_ENUM.options.map(({ value, label }) => ({
          key: value,
          label,
        }))}
        onChange={onActiveKeyChange}
      />
      <SearchForm {...searchProps} />
      <AekTable {...tableProps} />
    </PageLayout>
  )
}

export default connect(store => ({
  modelState: store[namespace],
  loading: store.loading,
}))(Index)
